/**
 * Created by Administrator on 2017/11/7.
 * Copyright 2017-2018 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.radar.util;

import java.text.DecimalFormat;

public class LocationUtil {

    private static DecimalFormat df6 = new DecimalFormat("#0.000000");
    /**
     * 求B点经纬度
     * @param A 已知点的经纬度，
     * @param distance   AB两地的距离  单位km
     * @param angle  AB连线与正北方向的夹角（0~360）
     * @return  B点的经纬度
     */
    final static double Rc=6378137;
    final static double Rj=6356725;
    double m_LoDeg,m_LoMin,m_LoSec;
    double m_LaDeg,m_LaMin,m_LaSec;
    double m_Longitude,m_Latitude;
    double m_RadLo,m_RadLa;
    double Ec;
    double Ed;

    public LocationUtil(double longitude,double latitude){
        m_LoDeg=(int)longitude;
        m_LoMin=(int)((longitude-m_LoDeg)*60);
        m_LoSec=(longitude-m_LoDeg-m_LoMin/60.)*3600;

        m_LaDeg=(int)latitude;
        m_LaMin=(int)((latitude-m_LaDeg)*60);
        m_LaSec=(latitude-m_LaDeg-m_LaMin/60.)*3600;

        m_Longitude=longitude;
        m_Latitude=latitude;
        m_RadLo=longitude*Math.PI/180.;
        m_RadLa=latitude*Math.PI/180.;
        Ec=Rj+(Rc-Rj)*(90.-m_Latitude)/90.;
        Ed=Ec*Math.cos(m_RadLa);
    }

    /**
     * 根据一个经纬度，长度，角度，计算另一个经纬度
     * @param A
     * @param distance
     * @param angle
     * @return
     */
    public static double[] getMyLatLng(LocationUtil A,double distance,double angle){//方法
        double dx = distance*Math.sin(Math.toRadians(angle));
        double dy= distance*Math.cos(Math.toRadians(angle));
        double bjd=Double.valueOf(df6.format((dx/A.Ed+A.m_RadLo)*180./Math.PI));
        double bwd=Double.valueOf(df6.format((dy/A.Ec+A.m_RadLa)*180./Math.PI));
        double[] doubles = new double[]{bjd, bwd};
//        String lnglat=bjd+","+bwd;
        return doubles;
    }

    //private static double EARTH_RADIUS = 6378.137;
    private static double EARTH_RADIUS = 6371.393;
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两个经纬度之间的距离
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 1000);
        return s;
    }

    /**
     *
     * @param lat_a 纬度1
     * @param lng_a 经度1
     * @param lat_b 纬度2
     * @param lng_b 经度2
     * 计算两个点的角度
     * @return
     */
    public static double getAngle1(double lat_a, double lng_a, double lat_b, double lng_b) {

        double y = Math.sin(lng_b-lng_a) * Math.cos(lat_b);
        double x = Math.cos(lat_a)*Math.sin(lat_b) - Math.sin(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
        double brng = Math.atan2(y, x);

        brng = Math.toDegrees(brng);
        if(brng < 0) {
            brng = brng + 360;
        }
        return 360-brng;

    }


    private static double gps2d(double lat_a, double lng_a, double lat_b, double lng_b)
    {
        double d = 0;
        lat_a=lat_a*Math.PI/180;
        lng_a=lng_a*Math.PI/180;
        lat_b=lat_b*Math.PI/180;
        lng_b=lng_b*Math.PI/180;

        d=Math.sin(lat_a)*Math.sin(lat_b)+Math.cos(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
        d=Math.sqrt(1-d*d);
        d=Math.cos(lat_b)*Math.sin(lng_b-lng_a)/d;
        d=Math.asin(d)*180/Math.PI;
//d = Math.round(d*10000);
        return d;
    }

    /**
     * 根据直角三角形斜边和角度，计算角度临边长度
     *
     * @param angle
     * @param hypotenuse
     * @return
     */
    public static double getNeighbour(double angle, double hypotenuse) {
        return Math.abs(Math.cos(angle) * hypotenuse);
    }



    public static double getAngle2(double lng_a, double lat_a, double lng_b, double lat_b) {

        double cosC = Math.cos(90 - lat_b) * Math.cos(90 - lat_a) + Math.sin(90 - lat_b) * Math.sin(90 - lat_a) * Math.cos(lng_b - lng_a);

        double sinC = Math.sqrt(1- Math.pow(cosC, 2));

        return Math.asin((Math.sin(90 - lat_b) * Math.sin(lng_b - lng_a))/sinC);

    }

    public static void main(String[] args) {

        double brng = getAngle1(34.754133,113.612630,34.763591,113.619659);
        double brng2 = getAngle1(34.754133,113.612630,34.745403,113.634158);
        double brng3 = getAngle1(34.754133,113.612630,34.741772,113.591394);
        double brng4 = getAngle1(34.754133,113.612630,34.761034,113.601449);

        double ebrng = getAngle2(113.612630,34.754133,113.619659,34.763591);
        double ebrng2 = getAngle2(113.612630,34.754133,113.634158,34.745403);
        double ebrng3 = getAngle2(113.612630,34.754133,113.591394,34.741772);
        double ebrng4 = getAngle2(113.612630,34.754133,113.601449,34.761034);

        System.out.println(brng);
        System.out.println(brng2);
        System.out.println(brng3);
        System.out.println(brng4);
        System.out.println("------------------------");
        System.out.println(ebrng);
        System.out.println(ebrng2);
        System.out.println(ebrng3);
        System.out.println(ebrng4);

//        double[] doubles = getMyLatLng(new LocationUtil(113.599100,34.749600), 1000, 360);
//        System.out.println(doubles[0]+"," +doubles[1]);
        /*
          1、标准站点跟周围小型站点的长度集合，获取长度最长的一个，拿到那个小型站点
          2、标准站点跟小型站点 经纬度计算 长度和小型站对标准站的角度
          3、根据长度和角度计算直角三角形的临边长
         */


    }
}
